package com.yimixian.app.rest.api;

import com.yimixian.app.model.Banner;
import com.yimixian.app.model.Category;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.SearchKeyResult;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * 分类右边的商品列表
 */
public class CategoryGoodsAPI extends YmxBaseAPI<Category> {
    public final static String CATEGORY_GOODS_GET = "/v5/categories/{category_id}/goods";

    private int mCategoryId = 0;
    private int storeId = 0;

    /**
     * @param callback
     */
    public CategoryGoodsAPI(int categoryId, int storeId, Callback<Response> callback) {
        super(callback);
        mCategoryId = categoryId;
        this.storeId = storeId;
    }

    public void categoryGoodsGet() {
        if(storeId > 0 && mCategoryId > 0)
            mYmxRestService.categoryGoodsGet(mCategoryId, storeId, getUserToken(), mCallback);
    }

    @Override
    public String getRelativeURL() {
        return CATEGORY_GOODS_GET;
    }

    @Override
    public Category parseJson(JSONObject jsonObject) {
        Category category = new Category();
        List<GoodsItem> goods = new ArrayList<>();
        List<Banner> banners = new ArrayList<>();
        if (jsonObject != null) {
            JSONArray goodsArray = jsonObject.optJSONArray("goods");
            if (goodsArray != null) {
                for (int i = 0; i < goodsArray.length(); i++) {
                    JSONObject goodObj = goodsArray.optJSONObject(i);
                    if (goodObj != null) {
                        GoodsItem goodsItem = new GoodsItem();
                        goodsItem.id = goodObj.optInt("id");
                        goodsItem.name = goodObj.optString("name");
                        goodsItem.imgRatio = goodObj.optInt("img_ratio");
                        goodsItem.img = goodObj.optString("img");
                        goodsItem.promotionImg = goodObj.optString("promotion_img");
                        goodsItem.descUrl = goodObj.optString("desc_url");
                        goodsItem.unitDesc = goodObj.optString("unit_desc");
                        goodsItem.saleStatus = goodObj.optString("sale_status");
                        goodsItem.stockCount = goodObj.optString("stock_count");
                        goodsItem.saleStatus = goodObj.optString("sale_status");
                        goodsItem.unitPrice = goodObj.optString("unit_price");
                        goodsItem.unitOldPrice = goodObj.optString("unit_old_price");
                        goodsItem.shouldBuyCount = goodObj.optInt("should_buy_count");
                        goodsItem.isBuyingGroup = goodObj.optInt("is_buying_group");
                        goodsItem.buyingGroupCount = goodObj.optInt("buying_group_count");
                        goodsItem.buyingGroupPrice = (float) goodObj
                                .optDouble("buying_group_price");
                        goodsItem.countZeroReason = goodObj.optString("count_zero_reason");
                        goodsItem.start_time = goodObj.optLong("start_time");
                        goods.add(goodsItem);
                    }
                }
            }
            category.goods = goods;
            JSONArray bannersArray = jsonObject.optJSONArray("banners");
            if (bannersArray != null) {
                for (int i = 0; i < bannersArray.length(); i++) {
                    JSONObject bannerObj = bannersArray.optJSONObject(i);
                    if (bannerObj != null) {
                        String image = bannerObj.optString("image");
                        String url = bannerObj.optString("url");
                        Banner banner = new Banner(image, url);
                        banners.add(banner);
                    }
                }
            }
            category.banners = banners;
            category.bannersImageRatio = jsonObject.optInt("banners_image_ratio");
        }
        return category;
    }
}
